/*
 * Copyright (C) 2014
 *
 * Brick Yang <printfxxx@163.com>
 *
 * This program is free software. You can redistribute it and/or
 * modify it as you like.
 */

/**
 * @file	sys_api.h
 * @brief	Header file for linux kernel compatible APIs in user space
 */

#ifndef _SYS_API_H_
#define _SYS_API_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <linux/types.h>

#define GFP_ATOMIC	0

#define __packed	__attribute__((__packed__))
#if __GNUC__ >= 7
#define fallthrough	__attribute__((__fallthrough__))
#else
#define fallthrough
#endif

#ifndef container_of
#define container_of(ptr, type, member) ({			\
	const typeof(((type *)0)->member) *__mptr = (ptr);	\
	(type *)((char *)__mptr - offsetof(type,member));})
#endif

#define likely(x)		__builtin_expect(!!(x), 1)
#define unlikely(x)		__builtin_expect(!!(x), 0)

#define pr_emerg(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#define pr_alert(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#define pr_crit(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#define pr_err(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#define pr_warn(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#define pr_notice(fmt, ...)	fprintf(stdout, fmt, ##__VA_ARGS__)
#define pr_info(fmt, ...)	fprintf(stdout, fmt, ##__VA_ARGS__)
#ifdef DEBUG
#define pr_debug(fmt, ...)	fprintf(stdout, fmt, ##__VA_ARGS__)
#endif

#define BUG() do {					\
	pr_emerg("BUG: failure at %s:%d/%s()!\n",	\
		__FILE__, __LINE__, __func__);		\
	abort();					\
} while (0)

#define __WARN() do {					\
	pr_warn("WARNING: at %s:%d/%s()\n",		\
		__FILE__, __LINE__, __func__);		\
} while (0)

#define BUG_ON(x) do {			\
	if (unlikely(x))		\
		BUG();			\
} while (0)

#define WARN_ON(x) ({			\
	int __ret_warn_on = !!(x);	\
	if (unlikely(__ret_warn_on))	\
		__WARN();		\
	unlikely(__ret_warn_on);	\
})

typedef __u8  u8;
typedef __u16 u16;
typedef __u32 u32;
typedef __u64 u64;

struct __una_u16 { u16 x; } __packed;
struct __una_u32 { u32 x; } __packed;
struct __una_u64 { u64 x; } __packed;

typedef struct {
	int counter;
} atomic_t;

typedef struct {
	__s64 counter;
} atomic64_t;

#if __BITS_PER_LONG == 64
typedef atomic64_t atomic_long_t;
#else
typedef atomic_t atomic_long_t;
#endif

typedef unsigned int gfp_t;

static inline __u32 rol32(__u32 word, unsigned int shift)
{
	return (word << (shift & 31)) | (word >> ((-shift) & 31));
}

static inline u32 __get_unaligned_cpu32(const void *p)
{
	const struct __una_u32 *ptr = (const struct __una_u32 *)p;
	return ptr->x;
}

static inline long atomic_long_read(const atomic_long_t *v)
{
	return __atomic_load_n(&v->counter, __ATOMIC_RELAXED);
}

static inline long atomic_long_read_acquire(const atomic_long_t *v)
{
	return __atomic_load_n(&v->counter, __ATOMIC_ACQUIRE);
}

static inline void atomic_long_set(atomic_long_t *v, long i)
{
	__atomic_store_n(&v->counter, i, __ATOMIC_RELAXED);
}

static inline void atomic_long_set_release(atomic_long_t *v, long i)
{
	__atomic_store_n(&v->counter, i, __ATOMIC_RELEASE);
}

static inline long atomic_long_cmpxchg(atomic_long_t *v, long old, long new)
{
	__atomic_compare_exchange_n(&v->counter, &old, new, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
	return old;
}

static inline long atomic_long_cmpxchg_acquire(atomic_long_t *v, long old, long new)
{
	__atomic_compare_exchange_n(&v->counter, &old, new, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
	return old;
}

#define jhash_size(n)	((u32)1 << (n))
#define jhash_mask(n)	(jhash_size(n) - 1)

#define __jhash_mix(a, b, c)			\
{						\
	a -= c;  a ^= rol32(c, 4);  c += b;	\
	b -= a;  b ^= rol32(a, 6);  a += c;	\
	c -= b;  c ^= rol32(b, 8);  b += a;	\
	a -= c;  a ^= rol32(c, 16); c += b;	\
	b -= a;  b ^= rol32(a, 19); a += c;	\
	c -= b;  c ^= rol32(b, 4);  b += a;	\
}

#define __jhash_final(a, b, c)			\
{						\
	c ^= b; c -= rol32(b, 14);		\
	a ^= c; a -= rol32(c, 11);		\
	b ^= a; b -= rol32(a, 25);		\
	c ^= b; c -= rol32(b, 16);		\
	a ^= c; a -= rol32(c, 4);		\
	b ^= a; b -= rol32(a, 14);		\
	c ^= b; c -= rol32(b, 24);		\
}

#define JHASH_INITVAL	0xdeadbeef

static inline u32 jhash(const void *key, u32 length, u32 initval)
{
	u32 a, b, c;
	const u8 *k = key;

	a = b = c = JHASH_INITVAL + length + initval;

	while (length > 12) {
		a += __get_unaligned_cpu32(k);
		b += __get_unaligned_cpu32(k + 4);
		c += __get_unaligned_cpu32(k + 8);
		__jhash_mix(a, b, c);
		length -= 12;
		k += 12;
	}

	switch (length) {
	case 12: c += (u32)k[11] << 24;	fallthrough;
	case 11: c += (u32)k[10] << 16;	fallthrough;
	case 10: c += (u32)k[9] << 8;	fallthrough;
	case 9:  c += k[8];		fallthrough;
	case 8:  b += (u32)k[7] << 24;	fallthrough;
	case 7:  b += (u32)k[6] << 16;	fallthrough;
	case 6:  b += (u32)k[5] << 8;	fallthrough;
	case 5:  b += k[4];		fallthrough;
	case 4:  a += (u32)k[3] << 24;	fallthrough;
	case 3:  a += (u32)k[2] << 16;	fallthrough;
	case 2:  a += (u32)k[1] << 8;	fallthrough;
	case 1:  a += k[0];
		 __jhash_final(a, b, c);
		 break;
	case 0:
		break;
	}

	return c;
}

static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
{
	u32 a, b, c;

	a = b = c = JHASH_INITVAL + (length<<2) + initval;

	while (length > 3) {
		a += k[0];
		b += k[1];
		c += k[2];
		__jhash_mix(a, b, c);
		length -= 3;
		k += 3;
	}

	switch (length) {
	case 3: c += k[2];	fallthrough;
	case 2: b += k[1];	fallthrough;
	case 1: a += k[0];
		__jhash_final(a, b, c);
		break;
	case 0:
		break;
	}

	return c;
}

static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
{
	a += initval;
	b += initval;
	c += initval;

	__jhash_final(a, b, c);

	return c;
}

static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
{
	return __jhash_nwords(a, b, c, initval + JHASH_INITVAL + (3 << 2));
}

static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
{
	return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
}

static inline u32 jhash_1word(u32 a, u32 initval)
{
	return __jhash_nwords(a, 0, 0, initval + JHASH_INITVAL + (1 << 2));
}

#endif	/* _SYS_API_H_ */
